package game;

import java.util.ArrayList;

import points.IPointsGranter;
import points.lastFirstLetterMatches;

import compCreateWord.CreateStandardWord;
import compCreateWord.ICreateWord;
import display.IDisplay;
import display.StandardConsoleDisplay;
import userInsertWord.IUserInsertWord;
import userInsertWord.UserConsoleWord;
import wordChecker.IWordChecker;
import wordChecker.Standard5Letters;

public class Game {

	private ArrayList<Player> playersList = new ArrayList<Player>();
	IWordChecker wordChecker = null;
	IUserInsertWord userInsertWord = null;
	ICreateWord createWord = null;
	IDisplay display = null;
	IPointsGranter pointsGranter = null;
	
	ResultsToString prepareResults = new ResultsToString();
	int rounds = 5;

	public Game(){
		this.wordChecker = new Standard5Letters();
		this.userInsertWord = new UserConsoleWord();
		this.createWord = new CreateStandardWord();
		this.display = new StandardConsoleDisplay();
		this.pointsGranter = new lastFirstLetterMatches();
	}
	
	public Game(IWordChecker wordChecker, IUserInsertWord userInsertWord, ICreateWord createWord, IDisplay display, IPointsGranter pointsGranter){
		this.wordChecker = wordChecker;
		this.userInsertWord = userInsertWord;
		this.createWord = createWord;
		this.display = display;
		this.pointsGranter = pointsGranter;
	}
	
	public void setRounds(int rounds) {
		this.rounds = rounds;
	}
	
	public void addPlayer( String name, boolean isCompPlayer ){
		playersList.add( new Player( name, isCompPlayer ) );
	}
	
	public void clearPlayersList(){
		playersList.clear();
	}
	
	private String lastWord = "";
	private String newWord = "";
	
	public void start(){
		lastWord = createWord.createWord(wordChecker, "");
		display.display(lastWord);
		display.display("");
		for( int i = 0; i < rounds; i++ ){
			
			for( Player p : playersList ){
				if( p.isCompPlayer() ){
					newWord = createWord.createWord(wordChecker, lastWord);
					display.display( newWord );
				}
				else{
					newWord = userInsertWord.userInsertWord(wordChecker);
				}
				pointsGranter.grantPoint(p, lastWord, newWord);
				lastWord = newWord;
			}
			
		}
		display.display("");
		display.display( prepareResults.prepareResults(playersList) );
	}
	
}
